package il.ac.braude.sona.ocsf.client;

import il.ac.braude.sona.database.ChangeRequest;
import il.ac.braude.sona.database.User;
import il.ac.braude.sona.ocsf.Mssg;
import il.ac.braude.sona.ocsf.P;
import il.ac.braude.sona.ocsf.server.Server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class Client extends AbstractClient {
	private HashMap<Long,ServerResponseListener> responseAwaiting = new HashMap<Long,ServerResponseListener>();
	private static Client instance;
	private Client(String host, int port) {
		super(host, port);
	}
	@Override
	protected void handleMessageFromServer(Object msg) {
		ServerResponseListener listener = responseAwaiting.get(((Mssg)msg).getID());
		if (listener==null) return;
		listener.onServerResponse((Mssg)msg);
		responseAwaiting.remove(((Mssg)msg).getID());
	}
	public void sendToServer(ServerResponseListener listener,Mssg mssg) throws IOException {
		responseAwaiting.put(mssg.getID(), listener);
		sendToServer(mssg);
	}
	public static Client getInstance() {
		return instance;
	}
	public static void connect(String host, int port) throws IOException {
		instance=new Client(host, port); 
		instance.openConnection();
	}
	

}
